package com.yf.stat.modules.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.excel.ExportExcel;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.api.exception.ServiceException;
import com.yf.stat.modules.course.dto.Student;
import com.yf.stat.modules.exam.dto.*;
import com.yf.stat.modules.exam.dto.request.StatAnalyseReqDTO;
import com.yf.stat.modules.exam.dto.request.StatExamReqDTO;
import com.yf.stat.modules.exam.dto.request.StatQuReqDTO;
import com.yf.stat.modules.exam.mapper.StatExamMapper;
import com.yf.stat.modules.exam.service.StatExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
* <p>
* 考试总体统计控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-09-11 11:10
*/
@Api(tags={"考试总体统计"})
@RestController
@RequestMapping("/api/stat/exam")
public class StatExamController extends BaseController {

    @Autowired
    private StatExamService baseService;

    @Autowired
    private StatExamMapper statExamMapper;

    @Autowired
    private ExportExcel exportExcel;

    /**
     * 查找考试总览
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找考试总览")
    @PostMapping("/overview")
    public ApiRest<StatExamTotalDTO> overview(@RequestBody BaseIdReqDTO reqDTO) {
        StatExamTotalDTO dto = baseService.findOverview(reqDTO.getId());
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "用户考试情况列表")
    @PostMapping("/user-paging")
    public ApiRest<IPage<StatExamUserDTO>> userPaging(@RequestBody PagingReqDTO<StatExamReqDTO> reqDTO) {

        //分页查询并转换
        IPage<StatExamUserDTO> page = baseService.userPaging(reqDTO);

        return super.success(page);
    }

    /**
     * 导出excel文件
     */
    @PostMapping("/user-export")
    public ApiRest exportFile(HttpServletResponse response, @RequestBody PagingReqDTO<StatExamReqDTO> reqDTO) {


        reqDTO.setCurrent(1);
        reqDTO.setSize(Integer.MAX_VALUE);

        // 导出文件名
        IPage<StatExamUserDTO> page = baseService.userPaging(reqDTO);

        // 导出数据
        exportExcel.export(response, StatExamUserDTO.class, "统计总览", page.getRecords());

        return success();
    }


    /**
     * 部门统计树
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "部门统计树")
    @PostMapping("/tree-list")
    public ApiRest<List<StatExamTotalDTO>> statTree(@RequestBody StatExamReqDTO reqDTO) {

        //分页查询并转换
        List<StatExamTotalDTO> list = baseService.listStatTree(reqDTO.getExamId());
        return super.success(list);
    }

    /**
     * 导出excel文件
     */
    @PostMapping("/tree-export")
    public ApiRest exportFile(HttpServletResponse response, @RequestBody StatExamReqDTO reqDTO) {


        String examId = reqDTO.getExamId();

        if(StringUtils.isBlank(examId)){
            throw new ServiceException("必须先选择一个考试才能导出！");
        }

        // 导出文件名
        List<StatExamTotalDTO> list = baseService.listExpandTree(examId);

        // 导出数据
        exportExcel.export(response, StatExamTotalDTO.class, "部门统计", list);

        return success();
    }


    /**
     * 考试错题统计
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "考试错题统计")
    @PostMapping("/error-qu-paging")
    public ApiRest<IPage<StatErrorQuDTO>> errorQuPaging(@RequestBody PagingReqDTO<StatQuReqDTO> reqDTO) {

        //分页查询并转换
        IPage<StatErrorQuDTO> page = baseService.errorQuPaging(reqDTO);

        return super.success(page);
    }


    /**
     * 导出考试错题统计
     * @param response
     * @param reqDTO
     * @return
     */
    @PostMapping("/error-qu-export")
    public ApiRest errorQuExport(HttpServletResponse response, @RequestBody PagingReqDTO<StatQuReqDTO> reqDTO) {


        //分页查询并转换
        reqDTO.setCurrent(1);
        reqDTO.setSize(Integer.MAX_VALUE);
        IPage<StatErrorQuDTO> page = baseService.errorQuPaging(reqDTO);

        // 导出数据
        exportExcel.export(response, StatErrorQuDTO.class, "考试错题统计", page.getRecords());
        return success();
    }


    /**
     * 控制台-考试统计
     * @return
     */
    @ApiOperation(value = "考试列表")
    @PostMapping("/dash-exam-list")
    public ApiRest<List<StatExamDashDTO>> dashExamList() {
        List<StatExamDashDTO> list = statExamMapper.dashExamList();
        return super.success(list);
    }

    /**
     * 学生个人成绩分析
     * @param statAnalyseExamStudentDTO
     * @return
     */
    @ApiOperation(value = "学生个人分析")
    @PostMapping("/student-ai")
    public ApiRest<StatAnalyseReqDTO> studentAI(@RequestBody StatStudentExamAnalyseDTO statAnalyseExamStudentDTO){
        StatAnalyseReqDTO statAnalyseReqDTO = baseService.studentAI(statAnalyseExamStudentDTO);
        return super.success(statAnalyseReqDTO);
    }


    @ApiOperation(value = "班级成绩分析")
    @PostMapping("/class-ai")
    public ApiRest classAI(@RequestBody StatStudentExamAnalyseDTO statStudentExamAnalyseDTO){
        StatAnalyseReqDTO statAnalyseReqDTO = baseService.classAI(statStudentExamAnalyseDTO);
        return super.success(statAnalyseReqDTO);
    }




}
